package com.spzx.cart.controller;

import com.spzx.cart.domain.CartInfo;
import com.spzx.cart.service.CartService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping
@Tag(name = "购物车模块")
public class CartController extends BaseController {

    @Resource
    CartService cartService;

    @InnerAuth
    @GetMapping("deleteCheckedCartInfo/{userId}")
    @Operation(summary = "内部调用接口：删除提交订单选中的购物项")
    public R deleteCheckedCartInfo(@PathVariable("userId")Long userId){
        return R.ok(cartService.deleteCheckedCartInfo(userId));
    }


    @InnerAuth
    @GetMapping("cartListByUserId/{userId}")
    @Operation(summary = "内部调用接口：查询用户购物车中选中的购物项列表")
    public R<List<CartInfo>> cartListByUserId(@PathVariable("userId")Long userId){
        return R.ok(cartService.cartListByUserId(userId));
    }
    ///deleteCart/8  delete
    @DeleteMapping("deleteCart/{skuId}")
    @Operation(summary = "删除指定购物项")
    public AjaxResult deleteCart(@PathVariable("skuId")Long skuId){
        cartService.deleteCart(skuId);
        return success();
    }

    ///clearCart  get
    @GetMapping("clearCart")
    @Operation(summary = "清空购物车")
    public AjaxResult clearCart(){
        cartService.clearCart();
        return success();
    }


    //allCheckCart/1
    @GetMapping("allCheckCart/{isChecked}")
    @Operation(summary = "修改所有的购物项选中状态")
    public AjaxResult allCheckCart(@PathVariable("isChecked")Integer isChecked){
        cartService.allCheckCart(isChecked);
        return success();
    }
    //checkCart/8/1
    @GetMapping("checkCart/{skuId}/{isChecked}")
    @Operation(summary = "修改购物项选中状态")
    public AjaxResult checkCart(@PathVariable("skuId")Long skuId,
                                @PathVariable("isChecked")Integer isChecked){
        cartService.checkCart(skuId,isChecked);
        return success();
    }
    //cartList
    @GetMapping("cartList")
    @Operation(summary = "查询购物车所有购物项")
    public AjaxResult cartList(){
        return success(cartService.cartList());
    }

    @GetMapping("addToCart/{skuId}/{skuNum}")
    @Operation(summary = "添加sku到购物车")
    public AjaxResult addToCart(@PathVariable("skuId")Long skuId,
                                @PathVariable("skuNum")Integer skuNum){
        cartService.addToCart(skuId,skuNum);
        return success();
    }

}
